//
//  CardActionView.m
//  Guazi
//
//  Created by koolearn on 11-6-30.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CardActionView.h"
#import "GuaziAppDelegate.h"
#import "CardSelectViewController.h"
#import "DMDetailViewController.h"


@implementation CardActionView

@synthesize message;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.message = nil;
        
        tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 20)];
        tipLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
        tipLabel.hidden = YES;
        tipLabel.shadowColor = [UIColor whiteColor];
        tipLabel.font = [UIFont systemFontOfSize:14];
        tipLabel.textColor = [UIColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.0f];
        [self addSubview:tipLabel];
        
        acceptButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
        [acceptButton setTitle:@"同意" forState:UIControlStateNormal];
        UIImage* image = [[UIImage imageNamed:@"button_green.png"] stretchableImageWithLeftCapWidth:8 topCapHeight:0];
        [acceptButton setBackgroundImage:image forState:UIControlStateNormal];
        acceptButton.frame = CGRectMake(10, 5, 140, 30);
        acceptButton.hidden = YES;
        [acceptButton addTarget:self action:@selector(accept) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:acceptButton];
        
        rejectButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
        [rejectButton setTitle:@"拒绝" forState:UIControlStateNormal];
        image = [[UIImage imageNamed:@"deleteButton.png"] stretchableImageWithLeftCapWidth:7 topCapHeight:0];
        [rejectButton setBackgroundImage:image forState:UIControlStateNormal];
        rejectButton.frame = CGRectMake(170, 5, 140, 30);
        rejectButton.hidden = YES;
        [rejectButton addTarget:self action:@selector(reject) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:rejectButton];
    }
    return self;
}


- (void) show
{
    if ( [kDMCodeAskCard compare:message.appendCode] == NSOrderedSame ) {
        //索要名片请求
        if ( message.senderId == [GuaziAppDelegate getAppDelegate].user.userId ) {
            //我发送的请求
            tipLabel.hidden = NO;
            rejectButton.hidden = YES;
            acceptButton.hidden = YES;
            
            if ( [kDMResultCardAccept compare:message.appendResult] == NSOrderedSame ) {
                tipLabel.text = @"您的请求已经被接受。名片已发送，请查收";
            }
            else if ( [kDMResultCardReject compare:message.appendResult] == NSOrderedSame ) {
                tipLabel.text = @"您的请求已经被拒绝";
            }
            else {
                tipLabel.text = @"您的请求已发送，请等待对方处理。";
            }
        }
        else {
            //我接收到的请求
            if ( [kDMResultCardAccept compare:message.appendResult] == NSOrderedSame ) {
                tipLabel.hidden = NO;
                rejectButton.hidden = YES;
                acceptButton.hidden = YES;
                tipLabel.text = @"您已经接受该请求，名片已发送给对方。";
            }
            else if ( [kDMResultCardReject compare:message.appendResult] == NSOrderedSame ) {
                tipLabel.hidden = NO;
                rejectButton.hidden = YES;
                acceptButton.hidden = YES;
                tipLabel.text = @"您已经拒绝了该请求。";
            }
            else {
                //还没有处理
                tipLabel.hidden = YES;
                rejectButton.hidden = NO;
                acceptButton.hidden = NO;
            }
        }
    }
    else {
        
    }
    
    [self setNeedsLayout];
}


- (void) setMessage:(DirectMessage *)dm
{
    [message release];
    message = [dm retain];
    [self show];
}


- (void) accept
{
    CardSelectViewController* controller = [[CardSelectViewController alloc] initWithStyle:UITableViewStylePlain];
    controller.message = message;
    [[[GuaziAppDelegate getAppDelegate] currentNavigationController] pushViewController:controller animated:YES];
    [controller release];
}


- (void) reject
{
    BOOL result = [TwitterClient rejectAskCardSync:[message askId] dmId:message.messageId message:@""];
    if ( result ) {
        message.appendResult = kDMResultCardReject;
        [self show];
    }
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


- (void)dealloc
{
    [message release];
    [tipLabel release];
    [acceptButton release];
    [rejectButton release];
    [super dealloc];
}

@end
